!macro __CacheUpdate

    StrCmp $CacheVersion "" SkipCacheUpdate
    StrCmp $CurrentCacheVersion "" BeginCacheUpdate
    StrCmp $CacheVersion $CurrentCacheVersion 0 BeginCacheUpdate

    ${DetailPrint} "The installed Cache version is still up-to-date."
    ${DetailPrint} "Cache update was skipped."
    Goto SkipCacheUpdate
  
    ;-------------------
  
    BeginCacheUpdate:
  
    ${DetailPrint} "Downloading Cache version $CacheVersion, please wait..."
    
    StrCmp $Hidden "0" ShowCacheProgress
    
    ${Download} "silent" "Cache version $CacheVersion" "$CacheURL" "$PLUGINSDIR\${CacheZIP}"
    
    Pop $R0
    StrCmp $R0 "OK" ExtractCache
    ${DetailPrint} "Unable to download Cache at this time, will retry later."
    Goto SkipCacheUpdate
    
    ShowCacheProgress:
    
    ${Download} "banner" "Cache version $CacheVersion" "$CacheURL" "$PLUGINSDIR\${CacheZIP}"
    
    Pop $R0
    StrCmp $R0 "OK" ExtractCache
    StrCmp $R0 "Cancelled" CancelledCacheDownload
    
    MessageBox MB_RETRYCANCEL|MB_ICONSTOP|MB_TOPMOST "Download failed: $R0$\nPlease check your internet connection and try again!" IDRETRY ShowCacheProgress
    Goto SkipCacheUpdate
  
    CancelledCacheDownload:
    MessageBox MB_OK|MB_ICONEXCLAMATION|MB_TOPMOST "Download was aborted by user!"
    Goto SkipCacheUpdate
    
    ExtractCache:
    
    ${DetailPrint} "Extracting files, please wait..."

    CreateDirectory "$PLUGINSDIR\polipo"
    
    !insertmacro ZIPDLL_EXTRACT "$PLUGINSDIR\${CacheZIP}" "$PLUGINSDIR\polipo" "<ALL>"
    Pop $0
    
    StrCmp $0 "success" SuccessfullyExtractedCache 0
    ${DetailPrint} "An error has occured while extracting the files ($0)"
    RMDir /r "$PLUGINSDIR\polipo" 
    
    StrCmp $Hidden "1" SkipCacheUpdate
    
    MessageBox MB_RETRYCANCEL|MB_ICONSTOP|MB_TOPMOST "Extraction failed: $0$\nPlease check your user permissions and try again." IDRETRY ExtractCache
    Goto SkipCacheUpdate
  
    SuccessfullyExtractedCache:
    Delete "$PLUGINSDIR\${CacheZIP}"
    
    ;-------------------

    IfFileExists "$PLUGINSDIR\polipo\polipo.exe" 0 MissingCacheFile
    Goto AllCacheFilesThere
  
    MissingCacheFile:
    ${DetailPrint} "Error: At least one required file is missing in the update!"
    RMDir /r "$PLUGINSDIR\polipo"
    
    Goto SkipCacheUpdate
  
    AllCacheFilesThere: 
    
    StrCpy $UpgradeNeeded "yes"
    
    ;StrCpy $FlashVersion $Revision
    
    SkipCacheUpdate:
    
!macroend

!macro __CacheInstall

    StrCmp $CacheVersion "" SkipCacheInstall
    StrCmp $CacheVersion $CurrentCacheVersion SkipCacheInstall
    
    ${DetailPrint} "Installing Cache, please wait..."
    
    CreateDirectory "$INSTDIR\polipo"
    CreateDirectory "$INSTDIR\polipo\cache"
    CopyFiles /SILENT "$PLUGINSDIR\polipo\*.*" "$INSTDIR\polipo"
    IfErrors 0 CacheCopySuccessful
    ${DetailPrint} "Failed to copy Cache files to the install folder!" 
    RMDir /r "$PLUGINSDIR\polipo"
        
    StrCmp $Hidden "1" SkipCacheInstall   
    
    CacheCopySuccessful:
    
    RMDir /r "$PLUGINSDIR\polipo"
  
    ;-------------------

    ${DetailPrint} "Saving Cache version information, please wait..."

    ClearErrors
    FileOpen $0 "$INSTDIR\cache.ver" w
    IfErrors SkipWriteCacheVersion
    FileWrite $0 $CacheVersion
    FileClose $0
 
    SkipWriteCacheVersion:
    SkipCacheInstall:

!macroend

!define CacheUpdate "!insertmacro __CacheUpdate"
!define CacheInstall "!insertmacro __CacheInstall"